﻿using Microsoft.SemanticKernel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AiDrawingWinform.Commands;
using MediatR;

namespace AiDrawingWinform
{
    public class CanvasPlugin
    {
        private readonly long[,] _canvas = new long[Constants.Width, Constants.Height];
        private readonly IMediator _mediator;

        public CanvasPlugin(IMediator mediator)
        {
            _mediator = mediator;
            for (var i=0; i< Constants.Width; i++)
            {
                for (var j = 0; j< Constants.Height; j++)
                {
                    _canvas[i, j] = Color.White.ToArgb();
                }
            }
        }

        [KernelFunction("clear_canvas")]
        [Description("清除画布上的内容")]
        public async Task ClearCanvasAsync(Kernel kernel)
        {
            for (var i = 0; i < Constants.Width; i++)
            {
                for (var j = 0; j < Constants.Height; j++)
                {
                    _canvas[i, j] = Color.White.ToArgb();
                }
            }

            //await _mediator.Send(new ClearCanvasCommand(_canvas));
            await _mediator.Send(new ClearCanvasCommand());

            await Task.CompletedTask;
        }

        [KernelFunction("draw_pixel")]
        [Description("在画布上绘制一个像素点，第一个参数x为像素点的x坐标，第二个参数y为像素点的y坐标，第三个参数为一个长整数，以ARGB的形式表示像素的颜色")]
        public async Task DrawPixelAsync(Kernel kernel,
            [Description("需要绘制的像素点的X坐标")]
            int x,
            [Description("需要绘制的像素点的Y坐标")]
            int y,
            [Description("像素点的颜色，ARGB格式")]
            long color)
        {
            if (x < 0 || x >= Constants.Width || y < 0 || y >= Constants.Height)
            {
                throw new ArgumentOutOfRangeException();
            }

            _canvas[x, y] = color;

            await _mediator.Send(new DrawPixelCommand(x, y, color));

            await Task.CompletedTask;
        }

        [KernelFunction("draw_horizontal_line")]
        [Description("在画布上绘制一条水平线，第一个参数x1为线段的起始点的x坐标，第二个参数x2为线段的终点的x坐标，第三个参数y为线段的y坐标，第四个参数为一个长整数，以ARGB的形式表示线段的颜色")]
        public async Task DrawHorizontalLineAsync(Kernel kernel,
            [Description("线段的起始点的X坐标")]
            int x1,
            [Description("线段的终点的X坐标")]
            int x2,
            [Description("线段的Y坐标")]
            int y,
            [Description("线段的颜色，ARGB格式")]
            long color)
        {
            for(var x = x1; x <= x2; x++)
            {
                _canvas[x, y] = color;
            }

            await _mediator.Send(new DrawHorizontalLineCommand(x1, x2, y, color));

            await Task.CompletedTask;
        }

        [KernelFunction("draw_vertical_line")]
        [Description("在画布上绘制一条垂直线，第一个参数y1为线段的起始点的y坐标，第二个参数y2为线段的终点的y坐标，第三个参数x为线段的x坐标，第四个参数为一个长整数，以ARGB的形式表示线段的颜色")]
        public async Task DrawVerticalLineAsync(Kernel kernel,
            [Description("线段的起始点的Y坐标")]
            int y1,
            [Description("线段的终点的Y坐标")]
            int y2,
            [Description("线段的X坐标")]
            int x,
            [Description("线段的颜色，ARGB格式")]
            long color)
        {
            for (var y = y1; y <= y2; y++)
            {
                _canvas[x, y] = color;
            }
            await _mediator.Send(new DrawVerticalLineCommand(x, y1, y2, color));
            await Task.CompletedTask;
        }
    }
}
